package com.atguigu.gulimall.scheduled;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.gulimall.common.utils.R;
import com.atguigu.gulimall.feign.CouponFeignService;
import com.atguigu.gulimall.feign.ProductFeignService;
import com.atguigu.gulimall.service.SeckillService;
import com.atguigu.gulimall.to.SeckillSkuRedisTo;
import com.atguigu.gulimall.vo.SkuInfoVo;
import com.atguigu.gulimall.vo.SkuKillSessionVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SeckillSkuSchedule {

    public static final String UPLOAD_KILL_PRODUCT = "upload_kill_product";

    @Autowired
    private SeckillService seckillService;

    @Autowired
    private RedissonClient redissonClient;

    // TODO 该方法应该是幂等的
    @Scheduled(cron = "0 * * * * ?")
    public void hello() {
        // 重复上架无需处理
        RLock lock = redissonClient.getLock(UPLOAD_KILL_PRODUCT);
        try {
            // 进行分布式锁
            lock.lock(10, TimeUnit.SECONDS);
            log.info("秒杀商品上架");
            seckillService.uploadSeckillSkuLatest3Days();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
}
